package com.zzdx.deployment.system.service.impl;

import com.zzdx.deployment.common.config.Global;
import com.zzdx.deployment.common.support.Convert;
import com.zzdx.deployment.common.utils.StringUtils;
import com.zzdx.deployment.common.utils.file.FileUtils;
import com.zzdx.deployment.system.domain.ProductResource;
import com.zzdx.deployment.system.domain.ProductVersion;
import com.zzdx.deployment.system.mapper.ProductVersionMapper;
import com.zzdx.deployment.system.service.IProductResourceService;
import com.zzdx.deployment.system.service.IProductVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 产品版本 服务层实现
 *
 * @author ruoyi
 * @date 2018-12-25
 */
@Service
public class ProductVersionServiceImpl implements IProductVersionService {
    @Autowired
    private ProductVersionMapper productVersionMapper;

    @Autowired
    private IProductResourceService productResourceService;

    /**
     * 查询产品版本信息
     *
     * @param id 产品版本ID
     * @return 产品版本信息
     */
    @Override
    public ProductVersion selectProductVersionById(Integer id) {
        return productVersionMapper.selectProductVersionById(id);
    }

    /**
     * 查询产品版本列表
     *
     * @param productVersion 产品版本信息
     * @return 产品版本集合
     */
    @Override
    public List<ProductVersion> selectProductVersionList(ProductVersion productVersion) {
        return productVersionMapper.selectProductVersionList(productVersion);
    }

    /**
     * 新增产品版本
     *
     * @param productVersion 产品版本信息
     * @return 结果
     */
    @Override
    public int insertProductVersion(ProductVersion productVersion) {
        return productVersionMapper.insertProductVersion(productVersion);
    }

    /**
     * 修改产品版本
     *
     * @param productVersion 产品版本信息
     * @return 结果
     */
    @Override
    public int updateProductVersion(ProductVersion productVersion) {
        return productVersionMapper.updateProductVersion(productVersion);
    }

    /**
     * 删除产品版本对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProductVersionByIds(String ids) {
        //获取关联的全部配置
        List<ProductResource> allProductResources = new ArrayList<>();

        String[] pIds = Convert.toStrArray(ids);
        if(pIds.length > 0){
            for (String pId: pIds) {
                ProductResource productResource = new ProductResource();
                productResource.setProductVersionId(Integer.parseInt(pId));
                List<ProductResource> productResources = productResourceService.selectProductResourceList(productResource);
                allProductResources.addAll(productResources);
            }
        }
        //先删除版本，再删除对应的资源
        int updatedRows = productVersionMapper.deleteProductVersionByIds(Convert.toStrArray(ids));
        //再删除项目版本对应的配置
        if(updatedRows > 0 && allProductResources.size()>0){
            StringBuilder idStrs = new StringBuilder();
            for (int i =0; i< allProductResources.size();i++){
                ProductResource productResource = allProductResources.get(i);
                if(i == 0){
                    idStrs.append(productResource.getId());
                }else{
                    idStrs.append(","+productResource.getId());
                }

                //删除资源文件
                String downloadPath = Global.getDownloadPath();
                String relativePath = productResource.getResourceRelativePath();
                String resourceName = productResource.getResourceName();
                downloadPath = StringUtils.appendIfMissing(downloadPath, "/");
                String wholePath = downloadPath + relativePath + "/" + resourceName;
                FileUtils.deleteFile(wholePath);
            }
            //删除记录
            productResourceService.deleteProductResourceByIds(idStrs.toString());
        }
        return updatedRows;
    }

    /**
     * 获取产品版本树
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> selectProductVersionTree() {
        List<ProductVersion> allProductVersion = productVersionMapper.selectProductVersionList(new ProductVersion());
        List<Map<String, Object>> trees = getTrees(allProductVersion, false);
        return trees;
    }

    /**
     * 对象转产品版本树
     *
     * @param productVersionList 产品版本列表
     * @param isCheck            是否需要选中
     * @return
     */
    public List<Map<String, Object>> getTrees(List<ProductVersion> productVersionList, boolean isCheck) {
        Set<String> productIdSet = new HashSet<>();
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < productVersionList.size(); i++) {
            ProductVersion productVersion = productVersionList.get(i);
            Integer productId = productVersion.getProductId();
            //若产品ID未加入，则添加，否则路过
            if(!productIdSet.contains(productId.toString())){
                Map<String, Object> productMap = new HashMap<String, Object>();
                productMap.put("id", productId);
                productMap.put("pId", 0);
                productMap.put("name", productVersion.getProductName());
                productMap.put("title", productVersion.getProductName());
                productIdSet.add(productId.toString());
                trees.add(productMap);
            }
            Map<String, Object> productVersionMap = new HashMap<String, Object>();
            productVersionMap.put("id", productVersion.getId());
            productVersionMap.put("pId", productId);
            productVersionMap.put("name", productVersion.getVersion());
            productVersionMap.put("title", productVersion.getVersion());
            if (isCheck && i == 0) {
                productVersionMap.put("checked", true);
            } else {
                productVersionMap.put("checked", false);
            }

            trees.add(productVersionMap);
        }
        return trees;
    }
}
